#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
调试脚本：专门测试大连到南京的联运方案
"""

import sys
import os
sys.path.append(os.path.dirname(__file__))

from liudang_realtime import (
    _resolve_province, 
    get_flight_data_hub_first, 
    get_flight_data_strict,
    build_flight_rail_combos,
    _norm_city,
    PROVINCE_CAPITAL_HUB,
    PROVINCE_ADJACENT_HUBS
)

def test_province_resolution():
    """测试省份识别"""
    print("=" * 60)
    print("测试省份识别")
    print("=" * 60)
    
    test_cities = ["合肥", "合肥南站", "大连", "大连交通大学"]
    
    for city in test_cities:
        province = _resolve_province(city)
        print(f"城市: {city} -> 省份: {province}")
    
    print(f"\n省份映射表:")
    print(f"PROVINCE_CAPITAL_HUB: {PROVINCE_CAPITAL_HUB}")
    print(f"PROVINCE_ADJACENT_HUBS: {PROVINCE_ADJACENT_HUBS}")

def test_flight_data():
    """测试航班数据获取"""
    print("\n" + "=" * 60)
    print("测试航班数据获取")
    print("=" * 60)
    
    origin = "大连"
    destination = "合肥"
    date = "2025-09-29"
    
    print(f"测试: {origin} -> {destination} ({date})")
    
    # 1. 测试直达航班
    print(f"\n1. 直达航班:")
    direct_flights = get_flight_data_strict(origin, destination, date)
    print(f"   直达航班数量: {len(direct_flights)}")
    for i, flight in enumerate(direct_flights[:3]):  # 只显示前3个
        print(f"   {i+1}. {flight.get('origin', '')} -> {flight.get('destination', '')} | {flight.get('dep_time', '')} - {flight.get('arr_time', '')} | ¥{flight.get('price', 'N/A')}")
    
    # 2. 测试南京航班
    print(f"\n2. 大连到南京航班:")
    nanjing_flights = get_flight_data_strict(origin, "南京", date)
    print(f"   南京航班数量: {len(nanjing_flights)}")
    for i, flight in enumerate(nanjing_flights[:3]):
        print(f"   {i+1}. {flight.get('origin', '')} -> {flight.get('destination', '')} | {flight.get('dep_time', '')} - {flight.get('arr_time', '')} | ¥{flight.get('price', 'N/A')}")
    
    # 3. 测试航班优先策略
    print(f"\n3. 航班优先策略:")
    hub_flights = get_flight_data_hub_first(origin, destination, date)
    print(f"   总航班数量: {len(hub_flights)}")
    
    # 分析航班类型
    direct_count = 0
    nearby_count = 0
    for flight in hub_flights:
        if flight.get('is_nearby'):
            nearby_count += 1
            print(f"   附近航班: {flight.get('origin', '')} -> {flight.get('destination', '')} | nearby_city: {flight.get('nearby_city', '')} | nearby_type: {flight.get('nearby_type', '')}")
        else:
            direct_count += 1
    
    print(f"   直达航班: {direct_count} 条")
    print(f"   附近航班: {nearby_count} 条")

def test_multimodal_combos():
    """测试联运方案构建"""
    print("\n" + "=" * 60)
    print("测试联运方案构建")
    print("=" * 60)
    
    origin = "大连"
    destination = "合肥"
    origin_address = "大连交通大学"
    destination_address = "合肥南站"
    date = "2025-09-29"
    
    # 获取航班数据
    flights = get_flight_data_hub_first(origin, destination, date)
    print(f"输入航班数量: {len(flights)}")
    
    # 构建联运方案
    combos = build_flight_rail_combos(
        destination, date, flights, 
        origin_address, origin, destination_address, 
        "price", "auto"
    )
    
    print(f"输出联运方案数量: {len(combos)}")
    
    for i, combo in enumerate(combos[:5]):  # 只显示前5个
        print(f"\n方案 {i+1}:")
        print(f"  类型: {combo.get('type', '')}")
        print(f"  总时间: {combo.get('total_time', '')} 小时")
        print(f"  总价格: ¥{combo.get('total_price', 'N/A')}")
        print(f"  换乘次数: {combo.get('transfers', 0)}")
        
        details = combo.get('details', [])
        print(f"  详情段数: {len(details)}")
        for j, detail in enumerate(details):
            print(f"    {j+1}. {detail.get('type', '')} | {detail.get('origin', '')} -> {detail.get('destination', '')} | {detail.get('dep_time', '')} - {detail.get('arr_time', '')}")

def main():
    """主测试函数"""
    print("开始调试大连到南京联运方案问题...")
    
    try:
        test_province_resolution()
        test_flight_data()
        test_multimodal_combos()
        
        print("\n" + "=" * 60)
        print("调试完成")
        print("=" * 60)
        
    except Exception as e:
        print(f"调试过程中出现错误: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()